﻿using MediatR;
using T.Materials.API.Application.Command.MaterialStatisticsCommand;
using T.Materials.Repository.MaterialsInterface;

namespace T.Materials.API.Application.CommandHandler.MaterialStatisticsCommandHandler
{
    /// <summary>
    /// 删除物资统计命令处理器
    /// </summary>
    public class DeletedMaterialStatisticsCommandHandler : IRequestHandler<DeletedMaterialStatisticsCommand, int>
    {
        /// <summary>
        /// 物资统计仓储接口
        /// </summary>
        protected readonly IMaterialStatisticsRepository materialStatisticsRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialStatisticsRepository">物资统计仓储接口</param>
        public DeletedMaterialStatisticsCommandHandler(IMaterialStatisticsRepository materialStatisticsRepository)
        {
            this.materialStatisticsRepository = materialStatisticsRepository;
        }

        /// <summary>
        /// 处理删除物资统计命令
        /// </summary>
        /// <param name="request"> 删除物资统计命令</param>
        /// <param name="cancellationToken"> 取消标记</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Handle(DeletedMaterialStatisticsCommand request, CancellationToken cancellationToken)
        {
            var res = await materialStatisticsRepository.GetModelAsync(request.MaterialStatisticsId);
            res.IsDeleted = true;
            res.DeletedBy = request.DeletedBy;
            res.DeletedDate = DateTime.Now;
            return await materialStatisticsRepository.UpdateAsync(res);
        }
    }
}
